const DEFAULT_OPTIONS = {
  fftSize: 512
};

export type AnalyserOptions = typeof DEFAULT_OPTIONS;

export class OriginAnalyser {
  audioContext: AudioContext;
  audioAnalyser: AnalyserNode;
  arr: Uint8Array = new Uint8Array();
  constructor(private _options: AnalyserOptions = DEFAULT_OPTIONS) {
    this.audioContext = new AudioContext();
    this.audioAnalyser = this.audioContext.createAnalyser();
    this.audioAnalyser.fftSize = this._options.fftSize;
    this.audioAnalyser.connect(this.audioContext.destination);
    this.arr = new Uint8Array(this.audioAnalyser.frequencyBinCount);
  }

  private _getAnalyserData() {
    this.audioAnalyser.getByteTimeDomainData(this.arr);
    return this.arr;
  }

  getData() {
    return this._getAnalyserData();
  }
}
